﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThuVien;
using System.Data;

namespace TranTekBE.KPI_NhanSu
{
    public partial class Chinh_Sua_Phan_Quyen : PagePopUp
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Load JS
            ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "LoadJS", "LoadJS();", true);
            

            if (!IsPostBack)
            {
                string get_id_user = FunctionsUtils.GetValueStringFromQueryString("iduser", "0");

                string str_sql_user = "SELECT * FROM tbl_UserKPI WHERE ID_USER = '" + get_id_user + @"'";
                DataSet sql_popup_edit_user = FunctionsUtils.Database2.ExecuteQuery(str_sql_user);
                if (sql_popup_edit_user != null)
                {
                    //
                    string ngay_vao_lam = sql_popup_edit_user.Tables[0].Rows[0]["NGAY_VAO_LAM"].ToString();
                    txtNgay_vao_lam1.Text = Convert.ToDateTime(ngay_vao_lam).ToShortDateString().ToString();
                    txtUserName.Text = 
                        sql_popup_edit_user.Tables[0].Rows[0]["HO_USER"].ToString() + " "
                        + sql_popup_edit_user.Tables[0].Rows[0]["TEN_USER"].ToString()
                        ;
                    //
                }
                // GET Cửa Hàng
                FunctionsUtils.GetDllStore(droplist_ThuocCuaHang);
            }
            
        }

        protected void btnEdit_PhanQuyen_Click(object sender, EventArgs e)
        {
            string get_id_user = FunctionsUtils.GetValueStringFromQueryString("iduser", "0");
            string chi_tieu = txtChiTieu.Text.ToString();
            int number_chi_tieu = 0;
            if (chi_tieu == "" || chi_tieu == null)
            {
                number_chi_tieu = 0;
            }
            else
            {
                number_chi_tieu = FunctionsUtils.ConvertToInt(chi_tieu, 0);
            }
            try
            {


                // Update thông tin nhân người dùng 
                string sql_update_user = @" UPDATE tbl_UserKPI
                SET
                PHAN_QUYEN = '" + droplist_PhanQuyen.SelectedValue.ToString() + @"',
                NGAY_VAO_LAM = '" + Convert.ToDateTime(txtNgay_vao_lam1.Text) + @"', 
                CUA_HANG = '" + droplist_ThuocCuaHang.SelectedValue.ToString() + @"',
                CHI_TIEU_DOANH_SO_THANG = " + number_chi_tieu + @" 
                WHERE ID_USER='" + get_id_user + @"'
                
                ";
                FunctionsUtils.Database2.ExecuteNonQuery(sql_update_user);
                ClosePopupScripManager(true);
            }
            catch (Exception ex)
            {
                ltAddErr.Text = ex.ToString();
            }
        }
    }
}